package yc.star.friend.controller;

import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import yc.star.common.constants.HttpConstants;
import yc.star.common.controller.BaseController;
import yc.star.common.domain.AppResult;
import yc.star.common.domain.vo.LoginUserVo;
import yc.star.friend.domain.user.dto.UserDTO;
import yc.star.friend.domain.user.dto.UserUpdateDTO;
import yc.star.friend.domain.user.vo.UserVO;
import yc.star.friend.service.IUserService;

@RequestMapping("/user")
@Tag(name = "用户接口")
@RestController
public class UserController extends BaseController {

    @Autowired
    private IUserService userService;

    @PostMapping("/sendCode")
    public AppResult<Void> sendCode (@RequestBody UserDTO userDTO) {
        boolean send = userService.sendCode(userDTO);
        return toAppResult(send);
    }

    @PostMapping("/code/login")
    public AppResult<String> codeLogin (@RequestBody UserDTO userDTO) {
        String token = userService.codeLogin(userDTO.getPhone(),userDTO.getCode());
        return AppResult.success(token);
    }

    @DeleteMapping("/logout")
    public AppResult<Void> logout (@RequestHeader(HttpConstants.AUTHENTICATION) String token) {
        boolean logout = userService.logout(token);
        return toAppResult(logout);
    }

    @GetMapping("/info")
    public AppResult<LoginUserVo> info (@RequestHeader(HttpConstants.AUTHENTICATION) String token) {
        return AppResult.success(userService.info(token));
    }

    @GetMapping("/detail")
    public AppResult<UserVO> detail () {
        return AppResult.success(userService.detail());
    }

    @PutMapping("/edit")
    public AppResult<Void> edit (@RequestBody UserUpdateDTO userUpdateDTO) {
        return toAppResult(userService.edit(userUpdateDTO));
    }

    @PutMapping("/head-image/update")
    public AppResult<Void> updateHeadImage(@RequestBody UserUpdateDTO userUpdateDTO) {
        return toAppResult(userService.updateHeadImage(userUpdateDTO.getHeadImage()));
    }

}